import java.util.HashSet;

/**
* @description: 817. 链表组件
* @author hewei
* @date 2022/8/29 15:50
* @version 1.0
*/

public class NumComponents {

    public int numComponents(ListNode head, int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        ListNode temp = head;
        int ans = 0;
        int count = 0;
        while (temp != null) {
            if (set.contains(temp.val)) ++count;
            else {
                if (count > 0) ans++;
                count = 0;
            }
            temp = temp.next;
        }
        ans += count > 0 ? 1 : 0;
        return ans;
    }
}
